<template>
  <div class="xtx-cart-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>购物车</XtxBreadItem>
      </XtxBread>
      <div class="cart" v-if="$store.getters['cart/validList'].length">
        <table>
          <thead>
            <tr>
              <th width="120">
                <XtxCheckbox :modelValue="$store.getters['cart/isCheckAll']" @change="checkAll">全选</XtxCheckbox>
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="item in $store.getters['cart/validList']" :key="item.skuId">
              <td><XtxCheckbox @change="checkOne(item.skuId, $event)" :modelValue="item.selected" /></td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/product/${item.id}`"><img :src="item.picture" alt="" /></RouterLink>
                  <div>
                    <p class="name ellipsis">{{ item.name }}</p>
                    <p class="attr">{{ item.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ item.nowPrice }}</p>
                <p v-if="item.price - item.nowPrice > 0">
                  比加入时降价 <span class="red">&yen;{{ item.price - item.nowPrice }}</span>
                </p>
              </td>
              <td class="tc">
                <XtxNumbox
                  @change="$event => changeCount(item.skuId, $event)"
                  :modelValue="item.count"
                  :min="1"
                  :max="item.stock"
                />
              </td>
              <td class="tc">
                <p class="f16 red">&yen;</p>
                {{ (item.nowPrice * 100 * item.count) / 100 }}
              </td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p><a class="green" @click="deleteCart(item.skuId)" href="javascript:;">删除</a></p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action" v-if="$store.getters['cart/validList'].length">
        <div class="batch">
          <XtxCheckbox :modelValue="$store.getters['cart/isCheckAll']" @change="checkAll">全选</XtxCheckbox>
          <a href="javascript:;" @click="batchDeleteCart()">删除商品</a>
        </div>
        <div class="total">
          共 {{ $store.getters['cart/validTotal'] }}件商品，已选择
          {{ $store.getters['cart/selectedTotal'] }}件，商品合计：
          <span class="red">¥ {{ $store.getters['cart/selectedAmount'] }}</span>
          <XtxButton type="primary">下单结算</XtxButton>
        </div>
      </div>
      <CartNone v-else></CartNone>
    </div>
  </div>
</template>
<script>
// import { ref } from 'vue'
// import CartTest from './components/cart-test.vue'
import Confirm from 'erabbit-ui/packages/components/Confirm'
import Message from 'erabbit-ui/packages/components/Message'
import CartNone from './components/cart-none.vue'
import { useStore } from 'vuex'
export default {
  name: 'XtxCartPage',
  components: { CartNone },
  setup() {
    // const count = ref(0)
    // return { count }
    // 1.商品列表展示
    // 1.1准备数据：已选择的商品列表，计算已经选择商品总件数，计算已经选择商品总数，全选按钮的状态

    // 2.商品状态的更改
    // 2.1修改购物车商品信息的mutations(已经有了)
    // 2.2定义一个actions修改商品信息
    // 2.3checkbox组件绑定change事件，把待修改状态和skuId传递给事件函数
    // 2.4在处理函数中调用actions函数
    const store = useStore()
    const checkOne = (skuId, selected) => {
      store.dispatch('cart/updateCart', { skuId, selected })
    }
    // 3.全选操作
    // 3.1单独定义一个全选与反选的actions,后台有专门的接口做这个事情
    // 3.2在actions中处理本地数据更新3.3
    // 遍历有效的商品列表，通过updateCart函数(mutations函数)修改所有商品的状态
    // 3.4绑定全选框得change事件
    const checkAll = selected => {
      store.dispatch('cart/checkAllCart', selected)
    }
    // 4.删除操作
    // 4.1actions  mutations都已经定义好了
    // 4.2绑定删除按钮的点击事件，弹出一个确认框，点击确认后调用actions
    // 4.3如果数据都删除了，显示没有商品的界面
    const deleteCart = skuId => {
      Confirm({ text: '您确认从购物车删除改商品吗？' })
        .then(() => {
          // 确认删除
          store
            .dispatch('cart/deleteCart', skuId)
            .then(() => {
              Message({ text: '删除成功', type: 'success' })
            })
            .catch(() => {
              Message({ text: '删除失败', type: 'error' })
            })
        })
        .catch(() => {})
    }
    // 批量删除
    const batchDeleteCart = () => {
      Confirm({ text: '您确定从购物车删除选中的商品吗？' })
        .then(() => {
          // 确定删除，调用定义得actions函数
          store
            .dispatch('cart/batchDeleteCart')
            .then(() => {
              Message({ text: '删除成功', type: 'success' })
            })
            .catch(() => {
              Message({ text: '删除失败', type: 'error' })
            })
        })
        .catch(e => {
          console.log(e)
        })
    }
    // 修改数量
    const changeCount = (skuId, count) => {
      store.dispatch('cart/updateCart', { skuId, count })
    }
    return { checkOne, checkAll, deleteCart, batchDeleteCart, changeCount }
  }
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: var(--price-color);
}
.green {
  color: var(--xtx-color);
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
